package com.example.UDP_measure;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;


//new String(packet.getData(),0,packet.getLength())
public class Histogram extends View {
    Paint paint;
    private String title;
    private String unit;
    private int[] Y;
    private int[] height;
    private int mul;
//在哪个view中展示，表头，单位，纵坐标，柱高，放大倍数
    public Histogram(Context context,String title,String unit,int[] Y,double[] H,int multiple){
        super(context);
        paint=new Paint();
        paint.setColor(Color.YELLOW);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(3);
        this.title=title;
        this.unit=unit;
        this.Y=new int[6];
        for(int i =0;i<6;++i){
            this.Y[i]=Y[i];
        }
        height=new int[10];
        for(int i=0;i<10;++i){
            height[i]=(int)H[i];
        }
        mul=multiple;
    }
    public Histogram(Context context,String title,String unit,int[] Y,int[] H,int multiple){
        super(context);
        paint=new Paint();
        paint.setColor(Color.YELLOW);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(3);
        this.title=title;
        this.unit=unit;
        this.Y=new int[6];
        for(int i =0;i<6;++i){
            this.Y[i]=Y[i];
        }
        height=new int[10];
        for(int i=0;i<10;++i){
            height[i]=H[i];
        }
        mul=multiple;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(30);
        //绘制标题
        canvas.drawText(title, 400, 30, paint);
        //绘制坐标
        canvas.drawLine(50, 100, 50, 400, paint);//绘制纵坐标
        canvas.drawLine(50, 400, 1000, 400, paint);//绘制横坐标

        paint.setTextSize(25);  //设置字体的大小
        canvas.drawText(unit, 20, 70, paint);

        //绘制纵坐标的刻度'-'和数据  Y
        int max;
        for (int i = 0; i < Y.length; i++) {
            //画纵坐标的刻度横线 "-"
            canvas.drawLine(50, 400 - (i+1)*50, 54, 400 - (i+1)*50, paint);
            //画纵坐标的刻度数据
            canvas.drawText(Y[i]+"", 10, 410 - (i+1)*50, paint);
        }

        //绘制横坐标的数据  数据之间相隔100
        int array2[] = {1,2,3,4,5,6,7,8,9,10};
        int[] distance_array = {0,100,200,300,400,500,600,700,800,900};
        for (int i = 0; i < array2.length; i++) {
            canvas.drawText(array2[i]+"", distance_array[i] + 82, 430, paint);
        }

        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);//填充画笔

        //画4个矩形柱状图并标明值  注意横坐标之间相隔100
        for(int i=0;i<10;++i){
            canvas.drawRect(new Rect(distance_array[i] + 70,400-(int)height[i]*mul,distance_array[i] + 110,400),paint);
            paint.setColor(Color.RED);
            canvas.drawText(height[i]+"", distance_array[i]+75, 390-(int)height[i]*mul, paint);
            paint.setColor(Color.BLUE);
        }
    }
}